package GameEngine;



/**
 * The StdPlayer class implements all methods that will be used 
 * to interact with the player.
 * 
 * This class uses the Player interface, which defines 
 * two methods (request and confirm). 
 * 
 * @see Player#request( Move opponentMove )
 * @see Player#confirm( Status boardStatus )
 */
@SuppressWarnings("unused")
public class StdPlayer implements Player {

    /**
     * Holds an instance of the Input reader class.
     * This avoids the creation of unnecessary instances.
     */
    private static Input feed = new Input();
    
    /**
     * Declares if a player is black or red.
     */
    private boolean black;

    /**
     * Holds the string representation of the color.
     */
    private String color;
    
    /**
     * Holds the last move of the opponent player.
     */
    private Move lastMove = null;

    private GameMessage gameMessage;
    /**
     * The default constructor initializes the input reader 
     * if needed and saves it to a static class property.
     * 
     * @see Input
     */
    public StdPlayer(boolean black,GameMessage gameMessage)
    {
        //set color mode
        this.black = black;

        // assign color value
        this.color = this.black ? "black" : "red";
        
        this.gameMessage = gameMessage;
        
        // output player up message
        System.out.println("Player " + this.color + " comes up!");
    }

    /**
     * This method handles the interaction between the 
     * player and the playing field.
     * 
     * @param opponentMove The last move of the opponent player.
     * @return Returns An object that represents the current move.
     * @throws java.lang.Exception
     */
    public Move request(Move opponentMove, int turnsLeft) 
    {

        // check if it is the first move
        if( opponentMove != null )
        {
            System.out.println("Got opponent move (" + opponentMove + "). ");
        }
        Message message = gameMessage.readMessageFromGui();
        
        // create Move object
        return message.getMove();
    }
    /**
     * Checks if an error occurred or if an move was illegal.
     * 
     * @param boardStatus Information of the last move.
     * @throws java.lang.Exception
     * @throws java.rmi.RemoteException
     */
    public void confirm(Status boardStatus) throws Exception
    {
        // check if last move is valid
        if (boardStatus.value() == Status.ILLEGAL_MOVE) {
            throw new Exception("Illegal move!");
        }

        // check if an error occurred
        if( boardStatus.value() == Status.ERROR )
        {
            throw new Exception("Error occurred!");
        }

        if (boardStatus.value() == Status.BLACK_WINS) {         // checks if black wins
            System.out.println("Black (@) wins!");

        }
        if (boardStatus.value() == Status.WHITE_WINS) {         // checks if red wins
            System.out.println("Red (#) wins!");
        }
        if (boardStatus.value() == Status.DRAW) {           // checks if a draw occurres
            System.out.println("Draw!");
        }
    }


	@Override
	public void setAlgorithm(int algType1) {
		// TODO Auto-generated method stub
		
	}
}
